package customer.web.beans;

import java.util.LinkedList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import org.primefaces.context.RequestContext;

import customer.ejb.eaos.InvitationManager;
import customer.ejb.eaos.PersonalizedTPInterface;
import dtos.InvitationDTO;
import dtos.PersonalizedTPDTO;


@ManagedBean(name="myTravelsBean")
@ViewScoped
public class MyTravelsBean {
	
	private DataModel<PersonalizedTPDTO> travels;
	private PersonalizedTPDTO selectedTravel;
	@EJB
	private PersonalizedTPInterface personalizedTPList;
	@EJB
	private InvitationManager invitationManager;
	@EJB
	private PersonalizedTPInterface persTPMgr;

	
	private InvitationDTO invitationDTO;
	private String invited;
	private int invitationID;
	private String username;
	
	public MyTravelsBean(){
		invitationDTO = new InvitationDTO();
	}

		public DataModel<PersonalizedTPDTO> getTravels(String username)
		{
		 //String username = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("user_ID");
			this.username = username;
			refreshTravels();
			return travels;
		}
		public void refreshTravels(){
			List<PersonalizedTPDTO> c = new LinkedList<>();
			for(PersonalizedTPDTO p : personalizedTPList.getTravels(username))
				c.add(p);
			travels = new ListDataModel<>(c);
		}
		
	 public DataModel<PersonalizedTPDTO> getTravels(){

			return travels;
	 }
	 
	 public PersonalizedTPDTO getSelectedTravel(){
		 return selectedTravel;
		 
	 }
	 
	 public void setSelectedTravel(PersonalizedTPDTO selectedTravel){
		 this.selectedTravel = selectedTravel;
		 //RequestContext.getCurrentInstance().openDialog("invitationDialog"); 
	 }
	 
	 public void confirmTravel(PersonalizedTPDTO travel){
		 personalizedTPList.confirmTravel(travel);
		 FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Success",  "Your travel has been successfully confirmed.");  
		 FacesContext.getCurrentInstance().addMessage(null, message);  
	 }

	 public void inviteFriend(String inviter, String invited, int travelID){
		 
		 invitationDTO.setInvited(invited);
		 this.invitationID = invitationManager.add(invitationDTO,travelID,invited,inviter);
		 FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Success",  "A mail have been sent to your friend with the following code : "+this.invitationID+".");  
		 FacesContext.getCurrentInstance().addMessage(null, message);  
	 
	 }
	 
	 public InvitationDTO getInvitationDTO(){
		 return invitationDTO;
	 }
	 public void setInvitationDTO(InvitationDTO invitationDTO){
		 this.invitationDTO = invitationDTO;
	 }
	 
	 public String getInvited(){
		 return this.invited;
	 }
	 public void setInvited(String invited){
		 this.invited = invited;
	 }
	 
	 public int getInvitationID(){
		 return invitationID;
	 }
	 
	 public String delete(PersonalizedTPDTO tp){
		 if (tp.getConfirmed()){
			 FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, "",  "You can't delete a travel you confirmed");  
			 FacesContext.getCurrentInstance().addMessage(null, message);  
			 return null;
		
		 }
		 persTPMgr.removePersTP(tp.getTravelID());
		 FacesMessage msg = new FacesMessage("Travel deleted");  
		 FacesContext.getCurrentInstance().addMessage(null, msg);
		 refreshTravels();
		 return "";
	 }
}
